package com.rfid.emergencywarehouse.presenter;

import android.text.TextUtils;

import com.rfid.base.base.BasePresenter;
import com.rfid.base.util.LogUtils;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.manager.CellTagManager;
import com.rfid.emergencywarehouse.net.NetApi;
import com.rfid.emergencywarehouse.net.entity.CreateStocklRes;
import com.rfid.emergencywarehouse.net.entity.QueryBarcodeReq;
import com.rfid.emergencywarehouse.net.entity.QueryBarcodeRes;
import com.rfid.emergencywarehouse.reader.RfidManager;
import com.rfid.emergencywarehouse.util.BeeperUtils;
import com.rfid.emergencywarehouse.view.IScanView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ReadTagPresenter<T0 extends IScanView> extends BasePresenter<T0> {

    protected ExecutorService mTagExecutor;
    protected boolean mHasShelf,mStartReading;

    public ReadTagPresenter() {
        setDisableShelf(true);
        mTagExecutor = Executors.newSingleThreadExecutor();
    }


    public void setDisableShelf(boolean disableShelf) {
        System.out.println("setDisableShelf = " + disableShelf);
        mHasShelf = disableShelf;
    }


    public void startScanRfid() {
        startScanRfid(false);
    }

    /**loop相同标签是否重复*/
    public void startScanRfid(boolean loop) {
        LogUtils.v("startScanRfid");
        changeReadingStatus(true);
        List<String> caches = new ArrayList<>();
        RfidManager.getInstance().start();
        mTagExecutor.execute(new Runnable() {
            @Override
            public void run() {
                while (mStartReading) {
                    if (mHasShelf) {
                        List<String> tags= RfidManager.getInstance().readTags();
                        for(String uid : tags) {
                            if (!loop) {
                                if (caches.contains(uid)) {
                                    continue;
                                }
                                caches.add(uid);
                            }
                            if(CellTagManager.INSTANCE.isCellTag(uid)) {
                                continue;
                            }
                            if(!mStartReading) {
                                break;
                            }
                            BeeperUtils.beep(BeeperUtils.BEEPER_SHORT);
                            boolean accept = onRfid(uid);
                            System.out.println("accept = " + accept);
                            if(accept) {
                                break;
                            }
                        }
                    } else {
                        List<String>uids = RfidManager.getInstance().readTags();
                        if(uids.size() > 0) {
                            String tag = RfidManager.getInstance().readCellTag(uids.get(0));
                            System.out.println("tag = " + tag);
                            if (!TextUtils.isEmpty(tag) && !CellTagManager.INSTANCE.isBookTag(uids.get(0))) {
                                CellTagManager.INSTANCE.add(uids.get(0));
                                mHasShelf = true;
                                onShelf(tag);
//                                stopScanRfid();
                                try {
                                    Thread.sleep(200);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        });
    }

    protected void changeReadingStatus(boolean b) {
        mStartReading = b;
    }

    public void stopScanRfid() {
        System.out.println("stopScanRfid");
        RfidManager.getInstance().stop();
        changeReadingStatus(false);
    }

    public boolean isReading() {
        return mStartReading;
    }


    protected void onShelf(String shelf) {
    }

    public void queryBarcodeInfo(String barcode) {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                List<String> barcodes = new ArrayList<>();
                barcodes.add(barcode);
                try {
                    QueryBarcodeRes res = NetApi.INSTANCE.queryBarcodes(barcodes);
                    List<QueryBarcodeRes.DataBean> beans = res.getData();
                    BarcodeInfo barcodeInfo = new BarcodeInfo();
                    barcodeInfo.setBarcode(barcode);
                    if (beans != null && beans.size() > 0) {
                        QueryBarcodeRes.DataBean bean = beans.get(0);
                        barcodeInfo.setNew(false);
                        barcodeInfo.setCreateTime(bean.getCreateTime());
                        barcodeInfo.setDisplayName(bean.getDisplayName());
                        barcodeInfo.setMaterialCategoryName(bean.getMaterialCategoryName());
                        barcodeInfo.setMaterialName(bean.getMaterialName());
                        barcodeInfo.setMaterialType(bean.getMaterialType());
                        barcodeInfo.setModifyUserName(bean.getModifyUserName());
                        barcodeInfo.setSkuModel(bean.getSkuModel());
                        barcodeInfo.setWareHouseAreaName(bean.getWareHouseAreaName());
                        barcodeInfo.setWareHouseAreaId(bean.getWareHouseAreaId());
                        barcodeInfo.setShapeStatus(bean.getShapeStatus());
                        barcodeInfo.setOutSide(bean.isOutSide());
                        barcodeInfo.setEnable(bean.isEnabled());
                    } else {
                        barcodeInfo.setNew(true);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onBarcodeQuery(barcodeInfo,true);
                            }
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    protected boolean onRfid(String epc) {
        System.out.println("onRfid epc = " + epc);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mView!= null) {
                    mView.onBarcode(epc,epc);
                } else {
                    System.out.println("view is null");
                }
            }
        });
        return false;
    }
}
